package com.sky.module.system.Department.controller;


import com.sky.module.system.Department.domain.entity.Department;
import com.sky.module.system.Department.domain.vo.DepartmentQueryVO;
import com.sky.module.system.Department.service.DepartmentService;
import com.sky.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zsg
 * @since 2023-02-20
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /***
     * @description 查询部门列表
     *
     * @return com.zsg. utils.Result<?>
     * @date 2023/2/21 15:57
     *
     */
    @GetMapping("/list")
    public Result<?> list(DepartmentQueryVO departmentQueryVO) {
//        调用查询部门列表方法
        List<Department> departmentList = departmentService.findDepartmentList(departmentQueryVO);
        return Result.ok(departmentList);
    }

//    @GetMapping("/parent/list")
//    public Result getParentDeparentment() {
////        调用查询部门列表方法
//        final List<Department> departmentList = departmentService.findParentDepartment();
//        return Result.ok(departmentList);
//    }

    /***
     * @description 查询上级部门列表
     *
     * @return com.zsg.utils.Result
     * @date 2023/2/21 16:17
     *
     */
    @GetMapping("/parent/list")
    public Result getParentDepartment() {
//                调用查询上级部门列表方法
        final List<Department> departmentList = departmentService.findParentDepartment();
//        返回数据
        return Result.ok(departmentList);
    }

    /***
     * @description 添加部门
     * @param department
     * @return com.zsg.utils.Result
     * @date 2023/2/21 16:21
     *
     */
    @PreAuthorize(("hasAuthority('sys:department:add')"))
    @PostMapping("/add")
    public Result add(@RequestBody Department department) {
        if (departmentService.save(department)) {
            return Result.ok("添加部门成功");
        }
        return Result.error().message("添加部门失败");
    }
    @PreAuthorize(("hasAuthority('sys:department:edit')"))

    @PutMapping("/update")
    public Result edit(@RequestBody Department department) {
        if (departmentService.updateById(department)) {
            return Result.ok("修改部门成功");
        }
        return Result.error().message("修改部门失败");
    }

    /***
     * @description 检查该部门下是否有子部门
     * @param id
     * @return com.zsg.utils.Result
     * @date 2023/2/21 16:33
     *
     */
    @PreAuthorize(("hasAuthority('sys:department:delete')"))

    @GetMapping("/check/{id}")
    public Result check(@PathVariable("id") Long id) {
//        创建条件构建对象

        if (departmentService.hasChildrenOfDepartment(id)) {
            return Result.exist().message("该部门下存在子部门，无法删除");
        }
        //        调用查询部门下是否存在用户的方法
        if (departmentService.hasUserOfDepartment(id)) {
            return Result.exist().message("该部门下存在子部门，无法删除");

        }
        return Result.ok().message("可以删除");
    }

    /***
     * @description 删除部门
     * @param id
     * @return com.zsg.utils.Result
     * @date 2023/2/21 16:36
     *
     */
    @PreAuthorize(("hasAuthority('sys:department:delete')"))

    @DeleteMapping("/delete/{id}")
    public Result add(@PathVariable Long id) {
        if (departmentService.removeById(id)) {
            return Result.ok("删除部门成功");
        }
        return Result.error().message("删除部门失败");
    }
}

